package com.kuaixueit.crm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.kuaixueit.crm.pojo.ResultVO;
import com.kuaixueit.crm.service.StaffService;

@RequestMapping("admin")
@Controller
public class AdminController {
	
	@Autowired
	private StaffService staffService;
	
	
	
	
	
	/**
	 * 分页查询管理员
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public ResultVO listAdmin(
			@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="3")Integer rows) {
		
		PageInfo info = new PageInfo();
		try {
			info = staffService.findAdmin(page, rows);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(0,null,"查询成功");
		}
		return new ResultVO(Integer.valueOf(""+info.getTotal()),info.getList(),"查询成功");
	}
	
	
	@RequestMapping("update")
	@ResponseBody
	public ResultVO updateAdmin(
			Integer id,
			Integer[] roids,
			String username,
			String password
			) {
		
		try {
			staffService.update(id,roids,username,password);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(0, null, "修改失败");
		}
		return new ResultVO(0, null, "修改成功");
		
	}
	
	
	/**
	 * 根据用户id查找角色id
	 * @param staffId
	 * @return
	 */
	
	@RequestMapping("roles")
	@ResponseBody
	public ResultVO findRoleByStaffID(Integer staffId) {
		List ids = new ArrayList();
		try {
			ids = staffService.findRoleIdsByStaffId(staffId);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(0, null, "查询失败");
		}
		return new ResultVO(0, ids, "查询成功");
	}

}
